package com.javawork.juc.four;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @author zhangxiaolu
 * @since 2023-02-11 23:55
 */
public class AtomicStampedDemo {

    public static void main(String[] args) {
        Book java = new Book(1, "java");
        AtomicStampedReference<Book>  atomicReference = new AtomicStampedReference<>(java, 1);
        System.out.println(atomicReference.getReference() + "\t" + atomicReference.getStamp());
        Book mysql = new Book(2, "mysql");
        boolean b;
        b = atomicReference.compareAndSet(java, mysql, atomicReference.getStamp(), atomicReference.getStamp() + 1);
        System.out.println(atomicReference.getReference() + "\t" + atomicReference.getStamp());
        b = atomicReference.compareAndSet(mysql, java, atomicReference.getStamp(), atomicReference.getStamp() + 1);
        System.out.println(atomicReference.getReference() + "\t" + atomicReference.getStamp());
    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Book {
    Integer id;
    String bookName;
}